import { Module } from 'vuex'
import GlobalDataProps from '../types'

export interface CachedView {
  cachedList: any[]
}
const cachedView: Module<CachedView, GlobalDataProps> = {
  namespaced: true,
  state: {
    cachedList: []
  },
  mutations: {
    setCachedView(state, view) {
      if (state.cachedList.includes(view.name)) return
      // 当设置 meta.noCache true 时 不会放进缓存
      if (view.meta.keepAlive) {
        state.cachedList.push(view.name)
      }
    },

    clearCachedItem(state, view) {
      const index = state.cachedList.indexOf(view.name)
      index > -1 && state.cachedList.splice(index, 1)
    },

    clearAllCachedView: state => {
      state.cachedList = []
    }
  }
}

export default cachedView
